package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.addDish(dish);

        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() != 0 && flavors != null) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            dishFlavorMapper.addFlavor(flavors);
        }
    }

    @Override
    public PageResult dishList(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<Dish> page = dishMapper.getList(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void setStatus(Integer id, Short status) {
        dishMapper.setStatus(id, status);
    }

    @Override
    public void deleteDishByIds(String ids) {
        dishMapper.deleteDishByIds(ids);
    }

    @Override
    public Dish getDish(Integer id) {
        Dish dish = dishMapper.getDish(id);
        return dish;
    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.updateDish(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() != 0 && flavors != null) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.addFlavor(flavors);
        }
    }

    @Override
    public List<DishVO> listWithFlover(Dish dish) {
        List<Dish> dishList = dishMapper.listWithFlover(dish);
        List<DishVO> dishVOS = new ArrayList<>();
        for (Dish dish1 : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish1, dishVO);
            dishVOS.add(dishVO);
        }
        for (DishVO dishVO : dishVOS) {
            List<DishFlavor> dishFlavors = dishFlavorMapper.getFlavorList(dishVO.getId());
            dishVO.setFlavors(dishFlavors);
        }
        return dishVOS;
    }

}
